package com.tjsinfo.tjsoa.address.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.address.inside.TAddresslistInside;
import com.tjsinfo.tjsoa.address.inside.TAddresslistInsideManager;
import com.tjsinfo.tjsoa.system.dao.ITSystemRolesGroupDAO;
import com.tjsinfo.tjsoa.system.dao.ITSystemUserRoleGroupDAO;
import com.tjsinfo.tjsoa.system.dao.TSystemUserRoleDAO;
import com.tjsinfo.tjsoa.system.vo.TSystemUserRole;
import com.tjsinfo.tjsoa.system.vo.TSystemUserRoleGroup;
import com.tjsinfo.tjsoa.system.vo.TjsRoles;
import com.tjsinfo.tjsoa.system.vo.User;

public class InsideListFindAction extends Action {
	
    TAddresslistInsideManager addresslistInsideManager;
    private ITSystemUserRoleGroupDAO TSystemUserRoleGroupDAO;
	private ITSystemRolesGroupDAO TSystemRolesGroupDAO;
	private TSystemUserRoleDAO TSystemUserRoleDAO;
	TSystemUserRoleGroup systemUserRoleGroup=null;
	TjsRoles sysRoles=null;
	TSystemUserRole systemUserRole=null;
    
	public TAddresslistInsideManager getAddresslistInsideManager() {
		return addresslistInsideManager;
	}

	public void setAddresslistInsideManager(
			TAddresslistInsideManager addresslistInsideManager) {
		this.addresslistInsideManager = addresslistInsideManager;
	}
	
	public ITSystemUserRoleGroupDAO getTSystemUserRoleGroupDAO() {
		return TSystemUserRoleGroupDAO;
	}

	public void setTSystemUserRoleGroupDAO(
			ITSystemUserRoleGroupDAO systemUserRoleGroupDAO) {
		this.TSystemUserRoleGroupDAO = systemUserRoleGroupDAO;
	}

	public ITSystemRolesGroupDAO getTSystemRolesGroupDAO() {
		return TSystemRolesGroupDAO;
	}

	public void setTSystemRolesGroupDAO(ITSystemRolesGroupDAO systemRolesGroupDAO) {
		this.TSystemRolesGroupDAO = systemRolesGroupDAO;
	}

	public TSystemUserRoleDAO getTSystemUserRoleDAO() {
		return TSystemUserRoleDAO;
	}

	public void setTSystemUserRoleDAO(TSystemUserRoleDAO systemUserRoleDAO) {
		TSystemUserRoleDAO = systemUserRoleDAO;
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {	
			request.setCharacterEncoding("UTF-8");
			
			HttpSession session=request.getSession();
			User tsui=(User)session.getAttribute("user");
						
			String chinesename=request.getParameter("chinese_name");
            String department=request.getParameter("department");
            String Id=request.getParameter("organizeId");
            String organizeId=Id.substring(1, Id.length()-1);
            String cid = request.getParameter("cid");
            
			if (cid.equals("1")){
				chinesename=new String(chinesename.getBytes("ISO8859_1"),"UTF-8");
				department=new String(department.getBytes("ISO8859_1"),"UTF-8");
			}
			
            request.setAttribute("chinese_name", chinesename);
            request.setAttribute("department", department);
            request.setAttribute("organizeId", organizeId);
            
            List<TAddresslistInside> inside=addresslistInsideManager.findList("chineseName","%"+chinesename+"%","department","%"+department+"%","organizeId",organizeId);
            request.setAttribute("inside", inside);
         
            Integer systemRoleId=10;
			Integer roleGroupId=0;
			String permission="";
			Integer userId=tsui.getUser_id();
			List<TSystemUserRoleGroup> RoleGroupId=TSystemUserRoleGroupDAO.findByuserId(userId);
			if(RoleGroupId.size()>0){
				for (int i=0;i<RoleGroupId.size();i++){
					    systemUserRoleGroup=RoleGroupId.get(i);
						roleGroupId=Integer.valueOf(systemUserRoleGroup.getRoleGroupId());
						permission=TSystemRolesGroupDAO.findById(roleGroupId).getPermission();
						if(permission.contains(","+systemRoleId+","))
					    	break;
					}
			}
			
			Integer roleId=0;
			List<TSystemUserRole> userRole=TSystemUserRoleDAO.findByProperty("userId", userId);
			if(userRole.size()>0){
				for (int i=0;i<userRole.size();i++){
					    systemUserRole=userRole.get(i);
					    roleId=Integer.valueOf(systemUserRole.getRoleId());
					    if(roleId==systemRoleId)
					    	break;
					}
			}
			
			if(permission.contains(","+systemRoleId+",")||roleId==systemRoleId){
				return mapping.findForward("success");
			}else {				
				return mapping.findForward("ok");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}
}
